﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    public partial class ChucVu : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Thembtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ChucVuThem";
                cmd.Parameters.AddWithValue("@machucvu", MaChucVutxb.Text);
                cmd.Parameters.AddWithValue("@tenchucvu", TenChucVutxb.Text);                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                ChucVugdv.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Suabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ChucVuSua";
                cmd.Parameters.AddWithValue("@machucvu", MaChucVutxb.Text);
                cmd.Parameters.AddWithValue("@tenchucvu", TenChucVutxb.Text); 
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                ChucVugdv.DataBind();
                Response.Write("Sửa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Xoabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ChucVuXoa";
                cmd.Parameters.AddWithValue("@machucvu", MaChucVutxb.Text);                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                ChucVugdv.DataBind();
                Response.Write("Xóa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow dong = ChucVugdv.SelectedRow;
            MaChucVutxb.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenChucVutxb.Text = HttpUtility.HtmlDecode(dong.Cells[2].Text);
        }
    }
}